package com.guo.hui.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.reflect.TypeToken;
import com.guo.hui.dao.ponal.CateDataMapper;
import com.guo.hui.dao.ponal.CateMapper;
import com.guo.hui.enumtype.DelFlag;
import com.guo.hui.model.ponal.Cate;
import com.guo.hui.service.ResumeCateService;
import com.guo.hui.util.UtilJson;

@Controller
@RequestMapping(value = "/resume/cate")
public class ResumeCateController {

	private static final String BASE_PATH = "resume/cate/";

	@Autowired
	private ResumeCateService cateService;
	@Autowired
	private CateMapper cateDao;

	@RequestMapping(value = "")
	public String cate() {
		return BASE_PATH + "cate";
	}

	@ResponseBody
	@RequestMapping(value = "/catedata")
	public String catedata() {
		Map<String, Object> map = new HashMap<>();
		List<Cate> list = cateDao.selectByMap(map);
		return UtilJson.toJson(list);
	}

	@RequestMapping(value = "/add")
	public String add() {
		return BASE_PATH + "add";
	}

	@RequestMapping(value = "/update")
	public String udpate(Model mm, @RequestParam Integer id) {
		Cate cate = cateDao.selectByKey(id);
		mm.addAttribute("cate", cate);
		return BASE_PATH + "update";
	}

	@ResponseBody
	@RequestMapping(value = "/savecate")
	public String savecate(@RequestParam String catejson) {
		Cate cate = (Cate) UtilJson.fromJson(catejson, Cate.class);
		cateService.saveCate(cate);
		return "1";
	}

	@ResponseBody
	@RequestMapping(value = "/updatecate")
	public String updatecate(@RequestParam String catejson) {
		Cate cate = (Cate) UtilJson.fromJson(catejson, Cate.class);
		cateService.updateCate(cate);
		return "1";
	}

	@ResponseBody
	@RequestMapping(value = "/deletecate")
	public String deletecate(@RequestParam String ids) {
		List<Integer> list = UtilJson.fromJsonType(ids, new TypeToken<List<Integer>>() {
		}.getType());
		List<Cate> listCate = cateDao.batchSelectList(list);
		for (Cate cate : listCate) {
			cate.setDelflag(DelFlag.LOSE.toString());
			cate.setModifyDate(new Date());
		}
		cateDao.batchUpdate(listCate);
		return "1";
	}

}
